package com.suny.sf_server.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.suny.AccountInfo.dto.*;
import com.suny.AccountInfo.vo.AccountVO;
import com.suny.sf_server.service.AccountInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;

import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.suny.AccountInfo.vo.AccountInfoVO;
import com.suny.result.PageDTO;
import com.suny.result.PageVO;
import com.suny.result.R;
import cn.dev33.satoken.stp.StpUtil;
import com.suny.exception.BusinessException;
@Slf4j
@Tag(name = "用户管理", description = "用户管理相关接口")
@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private AccountInfoService accountInfoService;

    @Operation(summary = "用户登录", description = "用户登录接口")
    @PostMapping("/login")
    public R login(@Parameter(description = "登录信息") @RequestBody LoginDTO loginDto) {
        AccountVO result = accountInfoService.login(loginDto);
        return R.OK(result);
    }

//    @Operation(summary = "获取账户列表", description = "分页获取账户列表")
//    @PostMapping("/getAccount")
//    public R getAccount(@Parameter(description = "分页参数") @RequestBody @Validated PageDTO pageDTO) {
//        try {
//            Page<AccountInfoVO> pageVO = accountInfoService.getAccount(pageDTO);
//
//            // 构造返回的分页数据
//            PageVO<AccountInfoVO> result = new PageVO<>();
//            result.setCurrent(pageVO.getCurrent());
//            result.setSize(pageVO.getSize());
//            result.setTotal(pageVO.getTotal());
//            result.setPages(pageVO.getPages());
//            result.setRecords(pageVO.getRecords());
//
//            return R.OK(result);
//        } catch (Exception e) {
//            return R.FAIL("获取账户列表失败");
//        }
//    }

    @Operation(summary = "用户登出")
    @GetMapping("/logout")
    public R logout() {
        // 检查是否登录
        StpUtil.logout();
        return R.OK("登出成功");
    }

    @Operation(summary = "新增用户", description = "新增用户信息")
    @PostMapping("/add")
    public R add(@Parameter(description = "用户信息") @Validated @RequestBody AccountInfoAddDTO accountInfoAddDTO) {
        try {
            Boolean result = accountInfoService.addAccountInfo(accountInfoAddDTO);
            if (!result) {
                return R.FAIL("新增用户失败");
            }
            return R.OK("新增用户成功");
        } catch (BusinessException e) {
            return R.FAIL(e.getMessage());
        } catch (Exception e) {
            return R.FAIL("新增用户失败");
        }
    }

    @Operation(summary = "修改用户")
    @PostMapping("/update")
    public R update(@Validated @RequestBody AccountInfoPutDTO accountInfoPutDTO) {
        try {
            if (accountInfoPutDTO.getAccount() == null) {
                return R.FAIL("用户账号不能为空");
            }
            Boolean result = accountInfoService.update(accountInfoPutDTO);
            if (!result) {
                return R.FAIL("修改用户失败");
            }
            return R.OK("修改用户成功");
        } catch (BusinessException e) {
            return R.FAIL(e.getMessage());
        } catch (Exception e) {
            return R.FAIL("修改用户失败");
        }
    }

    @Operation(summary = "条件查询人员列表")
    @PostMapping("/list")
    public R getListByCondition(@Validated @RequestBody AccountQueryDTO query) {
        log.info("接收到查询参数: {}", query);
        try {
            Page<AccountInfoVO> page = accountInfoService.getListByCondition(query);

            // 构造返回的分页数据
            PageVO<AccountInfoVO> result = new PageVO<>();
            result.setCurrent(page.getCurrent());
            result.setSize(page.getSize());
            result.setTotal(page.getTotal());
            result.setPages(page.getPages());
            result.setRecords(page.getRecords());

            log.info("查询结果: total={}, size={}", page.getTotal(), page.getRecords().size());
            return R.OK(result);
        } catch (BusinessException e) {
            log.error("查询失败", e);
            return R.FAIL(e.getMessage());
        }
    }

    @Operation(summary = "用户状态修改")
    @PostMapping("/updateStatus")
    public R updateStatus(@RequestBody @Validated AccountInfoUpdateStatusDTO dto) {
        try {
            String account = dto.getAccount();
            Integer status = dto.getStatus();
            Boolean result = accountInfoService.updateStatus(account, status);
            if (!result) {
                return R.FAIL("修改用户状态失败");
            }
            return R.OK("修改用户状态成功");
        } catch (BusinessException e) {
            return R.FAIL(e.getMessage());
        }
    }
}
